package cn.aifeix.org.controller;

import cn.aifeix.basic.util.AjaxResult;
import cn.aifeix.basic.util.LoginContext;
import cn.aifeix.basic.util.PageList;
import cn.aifeix.org.domain.Employee;
import cn.aifeix.org.query.EmployeeQuery;
import cn.aifeix.org.service.EmployeeService;
import cn.aifeix.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
//@LogAnno
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 添加/修改
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (null == employee.getId()){
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        employeeService.delete(id);
        return AjaxResult.me().success();
    }

    /**
     * 查询所有
     */
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /**
     * 根据id查询一个
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /**
     *
     * 分页查询
     */
    @PostMapping
//    @LogAnno(method = "queryByPage",methodDes = "分页查询员工")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        System.out.println(ids);
        employeeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据部门id查询员工
     */
    @GetMapping("/findByDeptId/{id}")
    @ApiOperation(value = "通过部门ID查询" )
    public List<Employee> findByDeptId(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long departmentId){
        return employeeService.findByDeptId(departmentId);
    }

    /**
     * 根据店铺id查询员工
     */
    @GetMapping("/findByShopId")
    public List<Employee> findByShopId(HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeService.findByLogininfoId(logininfo.getId());
        return employeeService.findByShopId(employee.getShopId());
    }
}
